<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Adminlogin extends Controller
{
    function adminlogin(Request $request)
    {
        return view('adminlogin');
    }
    function login_ok(Request $request)
    {

        $data = $request->input();

        $user = DB::table('admin_user')->where('username',$data['username'])->get();//查询符合后台用户输入的用户名

        foreach ($user as $user) {}

        $name=isset($user->userName)?$user->userName:"";

        if(!$name){//判断用户名
            return ['code'=>'-2','name'=>'用户名错误！点击确认跳转登录页面'];
//            return response()->json(['status' => 'success','code' => 200,'message' => '成功']);
        }

        $pwd=isset($user->password)?$user->password:"";

        if($pwd==$data['password']){//判断密码
            session(['username' => $user]);//存储session数据
            return ['code'=>'1','name'=>'验证成功！点击确认后跳转页面','url'=>'/admin'];
        }else{
            return ['code'=>'-1','name'=>'密码错误！点击确认跳转登录页面'];
        }
//        $url="/admin";
//        return redirect($url);
   }
   function login_out(Request $request){
       $request->session()->forget('username');
       return redirect('adminlogin');
   }
}
